	//
	//  JsBottomBar.m
	//  DuTe
	//
	//  Created by Gikki Ares on 2020/12/21.
	//  Copyright © 2020 vgemv. All rights reserved.
	//
#import "JsBottomInputBarBeta.h"
#import "JsBottomInputBarBetaVm.h"
#import <JsUtilitySdk/JsUtilitySdk.h>
#import "JsInputBox.h"

@interface JsBottomInputBarBeta()<
JsButtonLiiTwoDelegate,
JsButtonLiiOneDelegate,
JsInputBoxDelegate
>
{
	JsBottomInputBarBetaVm * vm_inputBar;
	
	JsButtonLiiOne * mjsButtonLii_voice;
	JsButtonLiiOne * mjsButtonLii_expression;
	JsButtonLiiTwo * mjsButtonLii_pick;
	JsButtonLiiOne * mbtn_share;
	JsButtonLiiOne * mjsButtonLii_like;
	
	JsInputBox * mInputBox;
	
	BOOL mb_isTextEmpty;
}
@end

const static int ButtonTag_Like = 1;
const static int ButtonTag_Share = 2;
const static int ButtonTag_Pick = 3;
const static int ButtonTag_Expression = 4;
const static int ButtonTag_Voice = 5;


@implementation JsBottomInputBarBeta

- (void)awakeFromNib {
	[super awakeFromNib];
	vm_inputBar = [JsBottomInputBarBetaVm new];
	[self commonInit];
}

- (instancetype)initWithFrame:(CGRect)frame vm:(JsBottomInputBarBetaVm *)vm {
	if(self = [super initWithFrame:frame]) {
	self.frame = frame;
	vm_inputBar = vm;
	[self commonInit];
	}
	return self;
}

//- (instancetype)initWithFrame:(CGRect)frame {
//	vm_inputBar = [JsBottomInputBarBetaVm new];
//	if(self = [super initWithFrame:frame vm:vm_inputBar]) {
//		[self commonInit];
//	}
//	return self;
//}

- (void)commonInit {
	mb_isTextEmpty = YES;
	[self initLikeBtn];
	[self initShareBtn];
	[self initPickBtn];
	[self initSeperator];
	[self initExpressionBtn];
	[self initVoiceBtn];
}

- (void)initLikeBtn {
	CGFloat x = vm_inputBar.mf_barWidth - vm_inputBar.mf_likeWidth - vm_inputBar.mf_likeTrailing;
	CGFloat y = (vm_inputBar.mf_barHeight - vm_inputBar.mf_likeHeight)/2;
	CGFloat w = vm_inputBar.mf_likeWidth;
	CGFloat h = vm_inputBar.mf_likeHeight;
	mjsButtonLii_like = [[JsButtonLiiOne alloc] initWithFrame:CGRectMake(x, y, w, h)];
	mjsButtonLii_like.tag = ButtonTag_Like;
	mjsButtonLii_like.delegate = self;
	JsButtonLiiVm * ui_like = [mjsButtonLii_like vm];
	ui_like.labelFont = [UIFont systemFontOfSize:8];
	
	[mjsButtonLii_like setTitle:@"0"];
	
	CGRect imageViewFrame = [JsGeometryManager frameHAlignCenterVAlignTopWithContainerSize:mjsButtonLii_like.frame.size size:CGSizeMake(20, 20) offset:CGPointMake(0, 7)];
	ui_like.foregroundImageViewFrame = imageViewFrame;
	

	CGRect labelFrame = [JsGeometryManager frameHAlignCenterVBelowWithContainerSize:mjsButtonLii_like.frame.size size:CGSizeMake(20, 20) siblingFrame:imageViewFrame offset:CGPointMake(0, 2)];
	
	
	ui_like.labelFrame = labelFrame;
	
	[self addSubview:mjsButtonLii_like];
	ui_like.labelTextColor = [UIColor redColor];
	UIImage * image_like = [JsBundleManager iconInBundleNamed:@"Heart_Red.png" inDir:@"Heart02"];
	if([ui_like.text isEqualToString:@"0"]) {
		image_like = [JsBundleManager iconInBundleNamed:@"Heart_Gray.png" inDir:@"Heart02"];
		ui_like.labelTextColor = [UIColor lightGrayColor];
	}
	[mjsButtonLii_like setImage:image_like];
	
	[mjsButtonLii_like setupUi];
	
}

- (void)initShareBtn {
	mbtn_share = [[JsButtonLiiOne alloc] initWithFrame:[vm_inputBar shareBtnFrame]];
	mbtn_share.tag = ButtonTag_Share;
	mbtn_share.delegate = self;
	[self addSubview:mbtn_share];
	JsButtonLiiVm * vm = [mbtn_share vm];
	vm.foregroundImageViewFrame = [JsGeometryManager frameAlignCenterWithContainerSize:mbtn_share.frame.size size:CGSizeMake(20,20)];
	UIImage * image = [JsBundleManager iconInBundleNamed:@"Share_Black.png"];
	[mbtn_share setImage:image];
	[mbtn_share setupUi];
	
}

- (void)initPickBtn {
	mjsButtonLii_pick = [[JsButtonLiiTwo alloc] initWithFrame:[vm_inputBar pickBtnFrame]];
	mjsButtonLii_pick.tag = ButtonTag_Pick;
	mjsButtonLii_pick.delegate = self;
	[self addSubview:mjsButtonLii_pick];
	JsButtonLiiVm * uip_unselected = [mjsButtonLii_pick unselectedVm];
	uip_unselected.foregroundImageViewFrame = [JsGeometryManager frameAlignCenterWithContainerSize:mjsButtonLii_pick.frame.size size:CGSizeMake(20, 20)];
	UIImage * unselectedImage = [JsBundleManager iconInBundleNamed:@"Star_BlackStroke.png"];
	uip_unselected.foregroundImage = unselectedImage;
	
	JsButtonLiiVm * uip_selected = [mjsButtonLii_pick selectedVm];
	uip_selected.foregroundImageViewFrame = [JsGeometryManager frameAlignCenterWithContainerSize:mjsButtonLii_pick.frame.size size:CGSizeMake(20, 20)];
	UIImage * selectedImage = [JsBundleManager iconInBundleNamed:@"Star_YellowFill.png"];
	uip_selected.foregroundImage = selectedImage;
	
	
	
		//放最后
	[mjsButtonLii_pick setupUi];
}

- (void)initSeperator {
	CGRect rc_pickBtnFrame = [vm_inputBar pickBtnFrame];
	CGFloat x = rc_pickBtnFrame.origin.x - vm_inputBar.mf_seperatorSpacing;
	CGFloat y = vm_inputBar.mf_seperatorTop;
	CGFloat w = vm_inputBar.mf_seperatorWidth;
	CGFloat h = vm_inputBar.mf_barHeight - y *2;
	UILabel * seperator = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
	seperator.backgroundColor = [UIColor lightGrayColor];
	[self addSubview:seperator];
}

- (void)initExpressionBtn {
	mjsButtonLii_expression = [[JsButtonLiiOne alloc] initWithFrame:[vm_inputBar expressionBtnFrame]];
	mjsButtonLii_expression.tag = ButtonTag_Expression;
	mjsButtonLii_expression.delegate = self;
	[self addSubview:mjsButtonLii_expression];
	JsButtonLiiVm * uip = [mjsButtonLii_expression vm];
	CGRect rect = [JsGeometryManager frameAlignCenterWithContainerSize:mjsButtonLii_pick.frame.size size:CGSizeMake(22, 22)];
	uip.foregroundImageViewFrame = rect;
	
	UIImage * image = [JsBundleManager iconInBundleNamed:@"Expression_Black.png"];
	[mjsButtonLii_expression setImage:image];
	
	[mjsButtonLii_expression setupUi];
}

- (void)initVoiceBtn {
	mjsButtonLii_voice = [[JsButtonLiiOne alloc] initWithFrame:[vm_inputBar voiceBtnFrame]];
	mjsButtonLii_voice.delegate = self;
	[self addSubview:mjsButtonLii_voice];
	mjsButtonLii_voice.tag = ButtonTag_Voice;
	JsButtonLiiVm * uip = [mjsButtonLii_voice vm];
	uip.mTextAlignment = NSTextAlignmentLeft;
	uip.labelTextColor = [UIColor lightGrayColor];
	uip.labelFont = [UIFont systemFontOfSize:15];
	[mjsButtonLii_voice setTitle:vm_inputBar.mstr_placeholder];
	
		//麦克风图标位置
	CGRect rect = [JsGeometryManager frameHAlignLeftVAlignCenterWithContainerSize:[vm_inputBar voiceBtnFrame].size size:CGSizeMake(22, 24) offset:CGPointMake(5, 0)];
	uip.foregroundImageViewFrame = rect;
		//右侧文字位置,左右各留一个padding就可以;
	int padding = 8;
	CGSize size = [JsGeometryManager sizeForOneLineStringWithContext:uip.text font:uip.labelFont];
	size.width = [vm_inputBar voiceBtnFrame].size.width - rect.origin.x - rect.size.width - padding * 2;
	rect = [JsGeometryManager frameHRightVAlignCenterWithContainerSize:[vm_inputBar voiceBtnFrame].size size:size siblingFrame:rect offset:CGPointMake(padding, 0)];
	uip.labelFrame = rect;
	
	UIImage * image = [JsBundleManager iconInBundleNamed:@"Microphone_Black.png"];
	[mjsButtonLii_voice setImage:image];
	
	[mjsButtonLii_voice setupUi];
}

#pragma mark 2 InnerFunction

#pragma mark 3 Interface
- (void)oneMoreLike {
	vm_inputBar.mi_likeCount ++;
	[self setLikeCount:vm_inputBar.mi_likeCount];
	[[JsLikeAnimationManager sharedInstance] showMoreLoveAnimateFromView:mjsButtonLii_like addToView:self.window];
}

- (void)setLikeCount:(int)count {
	JsButtonLiiVm * vm = [mjsButtonLii_like vm];
	vm_inputBar.mi_likeCount = count;
	vm.text = [NSString stringWithFormat:@"%d",vm_inputBar.mi_likeCount];
	vm.labelTextColor = [UIColor redColor];
	UIImage * image_like = [JsBundleManager iconInBundleNamed:@"Heart_Red.png" inDir:@"Heart02"];
	if([vm.text isEqualToString:@"0"]) {
		image_like = [JsBundleManager iconInBundleNamed:@"Heart_Gray.png" inDir:@"Heart02"];
		vm.labelTextColor = [UIColor lightGrayColor];
	}
	vm.foregroundImage = image_like;
	[mjsButtonLii_like setupUi];
}

- (JsBottomInputBarBetaVm *)getUiParameter {
	return vm_inputBar;
}

- (void)clickVoiceButton {
		//	[mjsButtonLii_voice toggle];
	[self jsButtonLiiOneClicked:mjsButtonLii_voice];
}

- (void)setPlaceholder:(NSString *)placeholder {
	vm_inputBar.mstr_placeholder = placeholder;
	NSString * text = vm_inputBar.mstr_text;
	if(text.length) {
		UIFont * font = [mjsButtonLii_voice vm].labelFont;
		NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"[草稿]%@",text] attributes:@{NSFontAttributeName:font}];
		[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
		[mjsButtonLii_voice updateAttributedText:str];
		
	}
	else {
		[mjsButtonLii_voice setTitle:vm_inputBar.mstr_placeholder];
	}
}


- (void)setDraftText:(NSString *)text {
	vm_inputBar.mstr_text = text;
	if(text.length) {
		NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"[草稿]%@",text] attributes:@{NSFontAttributeName:[mjsButtonLii_voice vm].labelFont}];
		[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
		[mjsButtonLii_voice updateAttributedText:str];
		
	}
	else {
		[mjsButtonLii_voice setTitle:vm_inputBar.mstr_placeholder];
	}
}

- (NSString *)draftText {
	if(vm_inputBar.mstr_text.length) {
		return vm_inputBar.mstr_text;
	}
	else {
		return nil;
	}
}


#pragma mark 4 Delegate
- (void)jsButtonLiiTwoClicked:(JsButtonLiiTwo *)button {
	switch (button.tag) {
		case ButtonTag_Pick:{
			if(_delegate&&[_delegate respondsToSelector:@selector(jsBottomInputBarBetaClickPick:)]) {
				[_delegate jsBottomInputBarBetaClickPick:self];
			}
			break;
		}
	}
}
- (void)jsButtonLiiOneClicked:(JsButtonLiiTwo *)button {
	switch (button.tag) {
		case ButtonTag_Like:{
			if(_delegate&&[_delegate respondsToSelector:@selector(jsBottomInputBarBetaClickLike:)]) {
				[_delegate jsBottomInputBarBetaClickLike:self];
			}
			break;
		}
		case ButtonTag_Share:{
			if(_delegate&&[_delegate respondsToSelector:@selector(jsBottomInputBarBetaClickShare:)]) {
				[_delegate jsBottomInputBarBetaClickShare:self];
			}
			break;
		}
		case ButtonTag_Expression:
		case ButtonTag_Voice:{
			[self awakeInputBox];
			break;
		}
			
	}
}



	//点击后的效果是固定的,可以集成进来.
- (void)jsBottomInputBarBetaClickExpress:(JsBottomInputBarBeta *)bar {
		//	[self gaoBottomBarClickVoice:bar];
}

- (void)awakeInputBox {
	JsInputBox * inputBox = [JsInputBox new];
	inputBox.delegate = self;
	NSString * text = [self draftText];
	if(text.length) {
		[inputBox setText:text];
	}
	[inputBox setPlaceholder:vm_inputBar.mstr_placeholder];
	[inputBox setNeedsLayout];
	
	
	
		//弹出inputBix
	
		//	[self.window addSubview:inputBox];
	JsDisplayStyle * style = [JsDisplayStyle defaulStyle];
	style.mb_isMaskCancellable = YES;
	style.displayPosition = JsDisplayPositionBottom;
	style.displayAnimationType = JsDisplayAnimationTypeNone;
	[inputBox displayInView:self.window withDisplayStyle:style undisplayCompletionHandler:^{
		NSString * text = [inputBox getText];
		[self setDraftText:text];
		[inputBox removeFromSuperview];
	}];
	
		//becomeFirsttResponder,会直接导致键盘通知被调用.所以,必须先让输入框的位置准备好.
	
		//这个runloop,计算再底部的位置,然后再动画.
	dispatch_async(dispatch_get_main_queue(),^{
		[inputBox becomeFirstResponder];
	});
}

#pragma mark JsInputBox
- (void)jsInputBox:(JsInputBox *)box publishContent:(NSString *)content {
	mInputBox = box;
	if(_delegate && [_delegate respondsToSelector:@selector(jsBottomInputBarBeta:publishContent:)]) {
		[_delegate jsBottomInputBarBeta:self publishContent:content];
	}
}

- (void)clearAfterPublish {
	[mInputBox endEditing:YES];
	[mInputBox undisplay];
	[mInputBox setText:@""];
	[self setDraftText:@""];
}


@end
